package com.googlecode.flightreports.flightlist;

import java.util.Iterator;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.googlecode.kohmori.reportgen.AbstractXMLBuilder;

public class ReportXMLBuilder extends AbstractXMLBuilder {
  
    public void parse(InputSource input) throws IOException, SAXException {
        handler.startDocument();
        generateFor((ReportObject)input);
        handler.endDocument();        
    }
    
    protected void generateFor(ReportObject report) throws SAXException {       
        handler.startElement("flightListByAirlineReport");

        handler.element("runDate", report.getRunDate(), runDateFormat);

        handler.startElement("queryParams");
        handler.element("airlineName", report.getQueryParams().getAirlineName());
        handler.element("sortedBy", report.getQueryParams().getSortedBy());
        handler.endElement("queryParams");

        handler.startElement("airlineFlights");
        Iterator i = report.getItems().iterator();
        while (i.hasNext()) {
        	ReportObject.Item item = (ReportObject.Item) i.next();
            generateFor(item);
        }
        handler.endElement("airlineFlights");
        handler.endElement("flightListByAirlineReport");
    }

    protected void generateFor(ReportObject.Item item) throws SAXException {
        handler.startElement("airlineFlight");
        handler.element("flightNumber", item.getFlightNumber());
        handler.element("origin", item.getOrigin());
        handler.element("destination", item.getDestination());
        handler.element("departTime", item.getDepartTime());
        handler.element("arriveTime", item.getArriveTime());
        handler.element("miles", item.getMiles());
        handler.element("numberOfSegments", item.getNumberOfSegments());
        handler.element("aircraftType", item.getAircraftType());
        handler.endElement("airlineFlight");
    }
}
